package com.transfiguro.datamodel.javamodels;

import com.transfiguro.datamodel.GenericStatement;
import japa.parser.ast.expr.Expression;
import japa.parser.ast.expr.VariableDeclarationExpr;

public class ForEachStatement extends GenericStatement {
    private Variable variable;
    private Variable iterator;
    private String loopBody;
    private static final String STATEMENT = "for";
    
    public Variable getVariable() {
        return variable;
    }

    public void setVariable(final VariableDeclarationExpr expression) {
        final Variable variable = new Variable();
        variable.setType(expression.getType().toString());
        variable.setVariableName(expression.getVars().get(0).toString());
        this.variable = variable;
    }

    public Variable getIterator() {
        return iterator;
    }

    public void setIterator(final Expression iterator) {
        final Variable variable = new Variable();

        //TODO Bad Initialization. Change
        variable.setType("");
        variable.setVariableName(iterator.toString());
        this.iterator = variable;
    }

    public String getLoopBody() {
        return loopBody;
    }

    public void setLoopBody(final String loopBody) {
        this.loopBody = loopBody;
    }

    //TODO Handle nulls
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append("for(").append(variable.getType()).append(' ').append(variable.getVariableName())
                .append(" : ").append(iterator.getVariableName()).append(")`")
                .append(loopBody!=null?loopBody:"{\n//This loop has no body\n}");

        return result.toString();
    }
}
